<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Core.php 2010-08-31 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Page_Plugin_Core {
	public function onRenderLayoutDefault($event) {
		$view = $event->getPayload ();
		
		if ($view instanceof Zend_View) {
			$view->headScript ()->appendFile ( 'application/modules/Page/externals/scripts/core.js' );
			
			$view->headTranslate ( array ('There was error.', 'Add admins', 'Pages sorted by', 'tag' ) );
		}
	}
	
	public function like($event) {
		$payload = $event->getPayload ();
		$page = $payload ['object'];
		$user = $payload ['user'];
		
		if ($page instanceof Page_Model_Page) {
			$page->getLikesList ()->add ( $user );
		}
	}
	
	public function unlike($event) {
		$payload = $event->getPayload ();
		$page = $payload ['object'];
		$user = $payload ['user'];
		
		if ($page instanceof Page_Model_Page) {
			$page->getLikesList ()->remove ( $user );
		}
	}
	
	public function onStatistics($event) {
		$table = Engine_Api::_ ()->getDbTable ( 'pages', 'page' );
		$select = new Zend_Db_Table_Select ( $table );
		$select->from ( $table->info ( 'name' ), array ('COUNT(*) AS count' ) )->where ( "name <> 'footer'" )->where ( "name <> 'header'" );
		$event->addResponse ( $select->query ()->fetchColumn ( 0 ), 'page' );
	}
	
	public function onUserDeleteBefore($event) {
		$payload = $event->getPayload ();
		if ($payload instanceof User_Model_User) {
			$table = Engine_Api::_ ()->getDbtable ( 'pages', 'page' );
			$select = $table->select ()->where ( 'user_id = ?', $payload->getIdentity () );
			foreach ( $table->fetchAll ( $select ) as $page ) {
				$page->delete ();
			}
		}
	}
}